
using namespace System.Management.Automation
using namespace System.Management.Automation.Language

Register-ArgumentCompleter -Native -CommandName 'envio' -ScriptBlock {
    param($wordToComplete, $commandAst, $cursorPosition)

    $commandElements = $commandAst.CommandElements
    $command = @(
        'envio'
        for ($i = 1; $i -lt $commandElements.Count; $i++) {
            $element = $commandElements[$i]
            if ($element -isnot [StringConstantExpressionAst] -or
                $element.StringConstantType -ne [StringConstantType]::BareWord -or
                $element.Value.StartsWith('-') -or
                $element.Value -eq $wordToComplete) {
                break
        }
        $element.Value
    }) -join ';'

    $completions = @(switch ($command) {
        'envio' {
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('profile', 'profile', [CompletionResultType]::ParameterValue, 'manage profiles')
            [CompletionResult]::new('set', 'set', [CompletionResultType]::ParameterValue, 'set or update environment variables in a profile')
            [CompletionResult]::new('unset', 'unset', [CompletionResultType]::ParameterValue, 'remove environment variables from a profile')
            [CompletionResult]::new('load', 'load', [CompletionResultType]::ParameterValue, 'load environment variables from a profile for use in the current terminal session')
            [CompletionResult]::new('unload', 'unload', [CompletionResultType]::ParameterValue, 'unload a profile from the current terminal session')
            [CompletionResult]::new('run', 'run', [CompletionResultType]::ParameterValue, 'run a command with profile environment variables')
            [CompletionResult]::new('import', 'import', [CompletionResultType]::ParameterValue, 'import a profile from a file or url')
            [CompletionResult]::new('export', 'export', [CompletionResultType]::ParameterValue, 'export a profile to a file')
            [CompletionResult]::new('version', 'version', [CompletionResultType]::ParameterValue, 'print version information')
            break
        }
        'envio;profile' {
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('new', 'new', [CompletionResultType]::ParameterValue, 'create a new profile')
            [CompletionResult]::new('delete', 'delete', [CompletionResultType]::ParameterValue, 'delete a profile')
            [CompletionResult]::new('list', 'list', [CompletionResultType]::ParameterValue, 'list all profiles')
            [CompletionResult]::new('show', 'show', [CompletionResultType]::ParameterValue, 'show environment variables in a profile')
            break
        }
        'envio;profile;new' {
            [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'description of the profile')
            [CompletionResult]::new('--description', 'description', [CompletionResultType]::ParameterName, 'description of the profile')
            [CompletionResult]::new('-f', 'f', [CompletionResultType]::ParameterName, 'file path to load environment variables from')
            [CompletionResult]::new('--from-file', 'from-file', [CompletionResultType]::ParameterName, 'file path to load environment variables from')
            [CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'environment variables to add (format: KEY=VALUE or only provide KEY and the value will be prompted for)')
            [CompletionResult]::new('--envs', 'envs', [CompletionResultType]::ParameterName, 'environment variables to add (format: KEY=VALUE or only provide KEY and the value will be prompted for)')
            [CompletionResult]::new('-k', 'k', [CompletionResultType]::ParameterName, 'encryption cipher to use')
            [CompletionResult]::new('--cipher-kind', 'cipher-kind', [CompletionResultType]::ParameterName, 'encryption cipher to use')
            [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'add comments to the provided environment variables')
            [CompletionResult]::new('--comments', 'comments', [CompletionResultType]::ParameterName, 'add comments to the provided environment variables')
            [CompletionResult]::new('-x', 'x', [CompletionResultType]::ParameterName, 'add expiration dates to the provided environment variables')
            [CompletionResult]::new('--expires', 'expires', [CompletionResultType]::ParameterName, 'add expiration dates to the provided environment variables')
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;profile;delete' {
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;profile;list' {
            [CompletionResult]::new('--no-pretty-print', 'no-pretty-print', [CompletionResultType]::ParameterName, 'disable pretty printing')
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;profile;show' {
            [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'display comments')
            [CompletionResult]::new('--show-comments', 'show-comments', [CompletionResultType]::ParameterName, 'display comments')
            [CompletionResult]::new('-x', 'x', [CompletionResultType]::ParameterName, 'display expiration dates')
            [CompletionResult]::new('--show-expiration', 'show-expiration', [CompletionResultType]::ParameterName, 'display expiration dates')
            [CompletionResult]::new('--no-pretty-print', 'no-pretty-print', [CompletionResultType]::ParameterName, 'disable pretty printing')
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;set' {
            [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'add comments to the provided environment variables')
            [CompletionResult]::new('--comments', 'comments', [CompletionResultType]::ParameterName, 'add comments to the provided environment variables')
            [CompletionResult]::new('-x', 'x', [CompletionResultType]::ParameterName, 'add expiration dates to the provided environment variables')
            [CompletionResult]::new('--expires', 'expires', [CompletionResultType]::ParameterName, 'add expiration dates to the provided environment variables')
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;unset' {
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;load' {
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;unload' {
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;run' {
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;import' {
            [CompletionResult]::new('-n', 'n', [CompletionResultType]::ParameterName, 'name for the imported profile')
            [CompletionResult]::new('--profile-name', 'profile-name', [CompletionResultType]::ParameterName, 'name for the imported profile')
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;export' {
            [CompletionResult]::new('-o', 'o', [CompletionResultType]::ParameterName, 'output file path (default: .env)')
            [CompletionResult]::new('--output-file-path', 'output-file-path', [CompletionResultType]::ParameterName, 'output file path (default: .env)')
            [CompletionResult]::new('-k', 'k', [CompletionResultType]::ParameterName, 'comma-separated list of keys to export (type ''select'' to choose interactively)')
            [CompletionResult]::new('--keys', 'keys', [CompletionResultType]::ParameterName, 'comma-separated list of keys to export (type ''select'' to choose interactively)')
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
        'envio;version' {
            [CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'show verbose version information')
            [CompletionResult]::new('--verbose', 'verbose', [CompletionResultType]::ParameterName, 'show verbose version information')
            [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
            [CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
            break
        }
    })

    $completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
        Sort-Object -Property ListItemText
}
